package Aleehot100;

/**
 * ClassName: c100
 * Package: Aleehot100
 * Description:287. 寻找重复数
 *
 * @Author BCXJ
 * @Create 2025/9/16 08:04
 * @Version 1.0
 * @Since 1.0
 */
public class c100 {
    public static void main(String[] args) {
        int[] arr = {3,1,3,4,2};

        System.out.println(findDuplicate(arr));
    }

    public static int findDuplicate(int[] nums) {
        // 快慢指针法
        int slow = nums[0];
        int fast = nums[0];
        // 两步走
        do {
            slow = nums[slow];
            fast = nums[nums[fast]];
        } while(slow != fast);
        // 一步走
        slow = nums[0];
        while(slow != fast) {
            slow = nums[slow];
            fast = nums[fast];
        }
        return slow;
    }
}
